//
//  SaveDocument.m
//  FashionCoachTest
//
//  Created by a on 2017/4/15.
//  Copyright © 2017年 zsr. All rights reserved.
//

#import "SaveDocument.h"

@implementation SaveDocument

+ (instancetype)shareSaveDocument
{
    static SaveDocument *shareDt = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        shareDt = [[SaveDocument alloc] init];
    });
    return shareDt;
}

- (void)saveDocuments:(NSArray *)saveArray Name:(NSString *)name
{
    NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
    NSString *documents = [array lastObject];
    NSString *documentPath = [documents stringByAppendingPathComponent:name];
    //第四步：准备好要存到本地的数组
    NSArray *dataArray = [NSArray arrayWithArray:saveArray];
    //第五步：将数组存入到指定的本地文件
    [dataArray writeToFile:documentPath atomically:YES];
    
}
- (NSArray *)readtheArrayName:(NSString *)name
{
    NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
    NSString *documents = [array lastObject];
    NSString *documentPath = [documents stringByAppendingPathComponent:name];
    return [NSArray arrayWithContentsOfFile:documentPath];
}

- (void)saveDocuments:(NSDictionary *)saveDic Dicname:(NSString *)name
{
    NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documents = [array lastObject];
    NSString *documnetPath = [documents stringByAppendingPathComponent:name];
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:0];
    //准备要存入的字典
    for (NSString *key in saveDic)
    {
        id value = [saveDic objectForKey:key];
        if (value == nil || value == NULL)
        {
            value = @"";
        }
        [dic setObject:value forKey:key];
    }
    //将字典存入指定的本地文件
    [dic writeToFile:documnetPath atomically:YES];
    //可对已经存储的字典操作
    NSDictionary *resultDic = [NSDictionary dictionaryWithContentsOfFile:documnetPath];
    NSLog(@"%@", documnetPath);
    
}

- (NSDictionary *)readtheDicName:(NSString *)name
{
    NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documents = [array lastObject];
    NSString *documnetPath = [documents stringByAppendingPathComponent:name];
    return [NSDictionary dictionaryWithContentsOfFile:documnetPath];
}

- (UserModel *)getUserDataModel:(NSString *)name
{
    NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documents = [array lastObject];
    NSString *documnetPath = [documents stringByAppendingPathComponent:name];
    NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:documnetPath];
    UserModel *model = [[UserModel alloc] init];
    model = [UserModel mj_objectWithKeyValues:dic];
    return model;
}

- (void)insertKey:(NSString *)key Value:(id)value Path:(NSString *)name
{
    if (value == nil || value == NULL)
    {
        value = @"";
    }
    NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documents = [array lastObject];
    NSString *documnetPath = [documents stringByAppendingPathComponent:name];
    NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:documnetPath];
    NSMutableDictionary *userDic = [NSMutableDictionary dictionaryWithDictionary:dic];
    [userDic setObject:value forKey:key];
    [dic writeToFile:documnetPath atomically:YES];
}


@end
